import java.util.Stack;

class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            //1. 遍历字符串
            for(int i = 0;i<s.length();i++){
                char ch = s.charAt(i);
                //2.是不是左括号
                if(ch == '(' || ch == '{' || ch == '['){
                    stack.push(ch);
                }else{
                    //3.右括号
                    //3.1 栈为空
                    if(stack.isEmpty()){
                        return  false;
                    }
                    //3.2 栈不为空
                    char ch2 = stack.peek();
                    if(ch == ')' && ch2 == '(' || ch == '}' && ch2 == '{'
                            || ch == ']' && ch2 == '['){
                        stack.pop();
                    }else {
                        return false;
                    }
                }
            }
            //字符串遍历完了 栈还是不为空
            if(!stack.isEmpty()){
                return  false;
            }
            return  true;
        }
    }

